<?php
/**
* 缓存管理测试页
*/
header('content-type:text/html; charset=utf-8');
class Admin_Page_Cache extends Admin_Page_Abstract
{
    private $cacheModuleCfg = array();
	
	/**
	* @param 
	*/
	public function validate(ME_Request $input, ME_Response $output)
	{
		$input->moduleName = trim($input->get('moduleName'));
        //$input->moduleId   = trim($input->get('moduleId'));
        $input->appId      = trim($input->get('appId')); //APP
        $input->classId    = trim($input->get('classId')); //APP

		$input->recursion  = (bool)$input->get('recursion');
		$input->ignore     = (array)$input->get('ignore');

        //配置
        $this->cacheModuleCfg = array(
                'Global_App' => array(
                            'param' => array(),
                        ),
                'Cms_Class' => array(
                            'param' => array('appId'=>$input->appId),
                        ),
                'Cms_SubClass' => array(
                            'param' => array('classId'=>$input->classId),
                        ),
             );



		return true;
	}
	
	/**
	* 显示
	*/
	public function doDefault()
	{

		$str = '<li><a href="?c=Cache&a=register&moduleName=">注册模块</a></li>';
		$str .= '<li><a href="?c=Cache&a=refresh&moduleName=">生成数据</a></li>';
		$str .= '<li><a href="?c=Cache&a=show&moduleName=">显示数据</a></li><br/>';
        
		$str .= '<li><a href="?c=Cache&a=Refresh&moduleName=Global_App">刷新APP</a></li>';
		$str .= '<li><a href="?c=Cache&a=Refresh&moduleName=Cms_Class">刷新频道</a></li>';

		echo $str;

        print_r($this->cacheModuleCfg);
		exit;
	}
	
	/**
	* 显示
	*/
	public function doShow(ME_Request $input)
	{
		if (!$input->moduleName) {
			die('ModuleName is empty!');
		}
		$param = array();
        if(isset($this->cacheModuleCfg[$input->moduleName])){
            $param = $this->cacheModuleCfg[$input->moduleName]['param'];
        }
        
		$cacheLoader = ME_DAL_GetCacheLoader::getInstance();
		$data = $cacheLoader->loadCacheObject($input->moduleName, $param);
		var_dump($data);
		exit;
	}
	
	
	
	/**
	* 注册
	*/
	public function doRegister(ME_Request $input)
	{
		$cacheModuleManager = ME_DAL_CacheModuleManager::getInstance();
		$cacheModuleManager->registerModule($input->moduleName, true)
		and die('注册“' . $input->moduleName . '”模块成功！')
		or die('注册“' . $input->moduleName . '”模块失败！');
	}
	
	/**
	* 生成
	*/
	public function doRefresh(ME_Request $input)
	{
		$param = array();
        if(isset($this->cacheModuleCfg[$input->moduleName])){
            $param = $this->cacheModuleCfg[$input->moduleName]['param'];
            /*if($tmpparam){
                foreach($tmpparam as $k => $v){
                    $param[$k] = $input->$k;
                }
            }*/
        }else{
            $param = array();
        }
        
		if ($input->recursion) {
			$param['recursion'] = 1;
		}
		$cacheRefreshLoader = ME_DAL_RefreshCacheLoader::getInstance();
		$starTime = time();
		$cacheRefreshLoader->refreshCacheObject($input->moduleName, $param)
		and die('批量刷新“' . $input->moduleName . '”模块成功！<br/>共用了<b color="red">' . 
			($useTime = (time() - $starTime)) . 's(' 
			. (floor($useTime/60) ? (floor($useTime/60) . '分钟') : '') 
			. (($useTime%60) ? ($useTime%60 . '秒') : '') . ')</b>')
		or die('刷新“' . $input->moduleName . '”模块失败！');
	}
}
